﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;

namespace mForum.Core.Infrastructure.Validation
{
    using mForum.Core.Infrastructure.Resource;

    public class ValidationHelper<T>
    {
        public ValidationHelper()
        {

        }

        public KeyValuePair<string, string> CreateErrorIf(bool condition, string errorKey, string errorMessageKey)
        {
            if (!condition)
                return new KeyValuePair<string, string>(errorKey, ResourceHelper.GetErrorMessage(errorMessageKey));
            else
                return new KeyValuePair<string, string>();
        }

        public KeyValuePair<string, string> CreateErrorIf<TResult>(bool condition, Expression<Func<T, TResult>> errorKey, string errorMessageKey)
        {
            return CreateErrorIf(condition, CreateFieldNameFromLambda(errorKey), errorMessageKey);
        }

        private string CreateFieldNameFromLambda<TResult>(Expression<Func<T, TResult>> errorKey)
        {
            var expressionMember = errorKey.Body as MemberExpression;

            return expressionMember.Member.Name;
        }
    }
}
